package com.qdsgjsfk.vision.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.base.BaseApplication;
import com.qdsgjsfk.vision.ui.fragment.StudentListFragment;
import com.rest.business.RestProxy;
import com.rest.response.IaBaseResponse;
import com.rest.response.Student;
import com.rest.response.StudentResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import com.qdsgjsfk.vision.base.BaseActivity;
import com.qdsgjsfk.vision.ui.dialog.RemarkDialog;
import com.qdsgjsfk.vision.util.IntentUtil;
import com.qdsgjsfk.vision.util.NetUtil;
import com.qdsgjsfk.vision.util.UtilString;

/**
 * Created by HP on 2019/9/2.
 */

public class SearchStudentActivity extends BaseActivity {

    @BindView(R.id.recyclerView_student_list)
    RecyclerView recyclerView_student_list;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;

    @BindView(R.id.ed_search)
    EditText ed_search;

    MyAdapter myAdapter;
    Bundle bundle;
    String taskId;
    List<Student> list = new ArrayList<>();
    private boolean hasAxisExamineOption, hasOtherExamineOption;

    @Override
    protected void initViews() {
        myAdapter = new MyAdapter();
        recyclerView_student_list.setLayoutManager( new LinearLayoutManager( this ) );
        recyclerView_student_list.setAdapter( myAdapter );
        refreshLayout.setRefreshFooter( new BallPulseFooter( this ).setSpinnerStyle( SpinnerStyle.Scale ) );
        refreshLayout.setRefreshHeader( new ClassicsHeader( this ) );
        refreshLayout.setEnableLoadMore( false );
        refreshLayout.setOnRefreshListener( new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getStudentList();
            }
        } );
        refreshLayout.setOnLoadMoreListener( new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {

            }
        } );
        ed_search.setOnEditorActionListener( new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                getStudentList();
                return true;
            }
        } );
    }

    @Override
    protected void initData() {
        taskId = getIntent().getStringExtra( "taskId" );
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_search;
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.StudentHolder> {

        @NonNull
        @Override
        public MyAdapter.StudentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new MyAdapter.StudentHolder( getLayoutInflater().inflate( R.layout.item_student, parent, false ) );
        }

        @Override
        public void onBindViewHolder(@NonNull MyAdapter.StudentHolder holder, @SuppressLint("RecyclerView") final int position) {
            ((StudentHolder) holder).tv_name.setText( list.get( position ).studentName );
            ((StudentHolder) holder).container.setBackgroundResource( R.drawable.round_white );

            if (list.get( position ).isCheck == 0) {
                ((StudentHolder) holder).img_vision.setImageResource( R.mipmap.unselected );
                ((StudentHolder) holder).img_diopter.setImageResource( R.mipmap.unselected );
            } else if (list.get( position ).isCheck == 1) {
                ((StudentHolder) holder).img_vision.setImageResource( R.mipmap.selected );
                ((StudentHolder) holder).img_diopter.setImageResource( R.mipmap.unselected );
            } else if (list.get( position ).isCheck == 2) {
                ((StudentHolder) holder).img_vision.setImageResource( R.mipmap.unselected );
                ((StudentHolder) holder).img_diopter.setImageResource( R.mipmap.selected );
            } else if (list.get( position ).isCheck == 3) {
                ((StudentHolder) holder).img_vision.setImageResource( R.mipmap.selected );
                ((StudentHolder) holder).img_diopter.setImageResource( R.mipmap.selected );
            }

            if (list.get( position ).checkAxis) {
                ((StudentHolder) holder).img_axis.setImageResource( R.mipmap.selected );
            } else {
                ((StudentHolder) holder).img_axis.setImageResource( R.mipmap.unselected );
            }

            if (list.get( position ).checkOther) {
                ((StudentHolder) holder).img_other.setImageResource( R.mipmap.selected );
            } else {
                ((StudentHolder) holder).img_other.setImageResource( R.mipmap.unselected );
            }


            if (hasAxisExamineOption) {
                ((StudentHolder) holder).ll_axis.setVisibility( View.VISIBLE );
            } else {
                ((StudentHolder) holder).ll_axis.setVisibility( View.INVISIBLE );
            }
            if (hasOtherExamineOption) {
                ((StudentHolder) holder).ll_other.setVisibility( View.VISIBLE );
            } else {
                ((StudentHolder) holder).ll_other.setVisibility( View.INVISIBLE );
            }

            if (!UtilString.isEmpty( list.get( position ).notExamReason )) {
                ((StudentHolder) holder).tv_reason.setVisibility( View.VISIBLE );
                ((StudentHolder) holder).tv_reason.setText( list.get( position ).notExamReason );
            } else {
                ((StudentHolder) holder).tv_reason.setVisibility( View.INVISIBLE );
            }

            ((StudentHolder) holder).itemView.setOnClickListener( (View.OnClickListener) v -> {

                if (hasOtherExamineOption) {
                    //有其他选项
                    IntentUtil.ToStudentDetailOtherActivity( SearchStudentActivity.this, list.get( position ).studentId, taskId, 0, list.get( position ).notExamReason );
                } else {
                    IntentUtil.ToStudentDetailActivity( SearchStudentActivity.this, list.get( position ).studentId, taskId, 0, list.get( position ).notExamReason );
                }


            } );

            ((StudentHolder) holder).ll_vision.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (hasOtherExamineOption) {
                        //有其他选项
                        IntentUtil.ToStudentDetailOtherActivity( SearchStudentActivity.this, list.get( position ).studentId, taskId, 0, list.get( position ).notExamReason );

                    } else {
                        IntentUtil.ToStudentDetailActivity( SearchStudentActivity.this, list.get( position ).studentId, taskId, 0, list.get( position ).notExamReason );
                    }

                }
            } );

            ((StudentHolder) holder).ll_diopter.setOnClickListener( v -> {
                if (hasOtherExamineOption) {
                    //有其他选项
                    IntentUtil.ToStudentDetailOtherActivity( SearchStudentActivity.this, list.get( position ).studentId, taskId, 1, list.get( position ).notExamReason );

                } else {
                    IntentUtil.ToStudentDetailActivity( SearchStudentActivity.this, list.get( position ).studentId, taskId, 1, list.get( position ).notExamReason );
                }

            } );

            ((StudentHolder) holder).ll_other.setOnClickListener( v -> {
                if (hasOtherExamineOption) {
                    //有其他选项
                    IntentUtil.ToStudentDetailOtherActivity( SearchStudentActivity.this, list.get( position ).studentId, taskId, 2, list.get( position ).notExamReason );

                } else {
                    IntentUtil.ToStudentDetailActivity( SearchStudentActivity.this, list.get( position ).studentId, taskId, 2, list.get( position ).notExamReason );
                }

            } );

            ((StudentHolder) holder).ll_axis.setOnClickListener( v -> {
                IntentUtil.ToStudentDetailOtherActivity( SearchStudentActivity.this, list.get( position ).studentId, taskId, 2, list.get( position ).notExamReason );

            } );
            ((StudentHolder) holder).ll_reason.setOnClickListener( v -> {
                RemarkDialog mRemarkDialog = null;

                if (mRemarkDialog == null) {
                    if (!UtilString.isEmpty( list.get( position ).notExamReason )) {
                        mRemarkDialog = mRemarkDialog.newInstance( true, true );
                    } else {
                        mRemarkDialog = mRemarkDialog.newInstance( false, true );
                    }
                    mRemarkDialog.show( getSupportFragmentManager(), "mRemarkDialog" );
                    RemarkDialog finalMRemarkDialog = mRemarkDialog;
                    final String[] notExamReason = new String[1];
                    mRemarkDialog.setmListener( new RemarkDialog.OnItemClickListener() {
                        @Override
                        public void onConfirm(String reason, int typeConfirm) {
                            if (!UtilString.isEmpty( reason )) {
                                notExamReason[0] = reason;
                                ((StudentHolder) holder).tv_reason.setVisibility( View.VISIBLE );
                                ((StudentHolder) holder).tv_reason.setText( reason );
                            } else {
                                //清空
                                notExamReason[0] = "";
                                ((StudentHolder) holder).tv_reason.setVisibility( View.INVISIBLE );
                            }
                            RestProxy.getInstance().notExamReason( taskId, list.get( position ).studentId, notExamReason[0], new Observer<IaBaseResponse>() {
                                @Override
                                public void onSubscribe(Disposable d) {

                                }

                                @Override
                                public void onNext(IaBaseResponse iaBaseResponse) {
                                    finalMRemarkDialog.dismiss();
                                    getStudentList();
                                }

                                @Override
                                public void onError(Throwable e) {
                                    finalMRemarkDialog.dismiss();
                                    e.printStackTrace();
                                    NetUtil.onError( e, SearchStudentActivity.this );
                                }

                                @Override
                                public void onComplete() {

                                }
                            } );


                        }
                    } );
                } else if (!mRemarkDialog.isAdded() && !mRemarkDialog.isShowing()) {
                    mRemarkDialog.show( getSupportFragmentManager(), "mRemarkDialog" );
                }
            } );


            if (list.get( position ).dataLegal) {
                ((StudentHolder) holder).img.setVisibility( View.VISIBLE );
            } else {
                ((StudentHolder) holder).img.setVisibility( View.INVISIBLE );
            }
            ((StudentHolder) holder).tv_class.setText( list.get( position ).classNameStr );
            ((StudentHolder) holder).tv_class.setVisibility( View.VISIBLE );
        }


        @Override
        public int getItemCount() {
            return list.size();
        }


        class StudentHolder extends RecyclerView.ViewHolder {
            TextView tv_name, tv_reason, tv_class;
            ImageView img, img_vision, img_diopter, img_diagnose, img_axis, img_other;
            LinearLayout ll_vision, ll_diopter, ll_axis, ll_other, container, ll_reason;
            View line;

            StudentHolder(View itemView) {
                super( itemView );
                tv_name = itemView.findViewById( R.id.tv_name );
                tv_class = itemView.findViewById( R.id.tv_class );
                tv_reason = itemView.findViewById( R.id.tv_reason );
                img = itemView.findViewById( R.id.img );
                img_vision = itemView.findViewById( R.id.img_vision );
                img_diopter = itemView.findViewById( R.id.img_diopter );
                img_axis = itemView.findViewById( R.id.img_axis );
                img_other = itemView.findViewById( R.id.img_other );
                container = itemView.findViewById( R.id.container );
                line = itemView.findViewById( R.id.line );
                ll_vision = itemView.findViewById( R.id.ll_vision );
                ll_diopter = itemView.findViewById( R.id.ll_diopter );
                ll_axis = itemView.findViewById( R.id.ll_axis );
                ll_other = itemView.findViewById( R.id.ll_other );
                ll_reason = itemView.findViewById( R.id.ll_reason );
            }
        }


    }

    private void getStudentList() {
        RestProxy.getInstance().getStudentList( taskId, "", 0 + "", ed_search.getText().toString().trim(), new Observer<StudentResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(StudentResponse studentResponse) {
                list.clear();
                list.addAll( studentResponse.data.classStudentList.list );
                myAdapter.notifyDataSetChanged();
                if (studentResponse.data.axisOption != null) {
                    hasAxisExamineOption = studentResponse.data.axisOption.hasAxisExamineOption;
                }
                if (studentResponse.data.otherOption != null) {
                    hasOtherExamineOption = studentResponse.data.otherOption.hasOtherExamineOption;
                }
                if (list.size() == 0) {
                    Toast.makeText( SearchStudentActivity.this, "暂无学生", Toast.LENGTH_SHORT ).show();
                }
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                NetUtil.onError( e, SearchStudentActivity.this );
                refreshLayout.finishRefresh();
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh();
            }
        } );
    }


}
